<template>
  <div class="page-content">
    <div class="header">
      <h3>设置</h3>
    </div>

    <el-form label-width="295px" style="margin-top: 20px">
      <el-form-item label="群名称">
        <el-input v-model="dataInfo.group_name" :disabled="!isEdit" />
      </el-form-item>
      <el-form-item label="群备注">
        <el-input v-model="dataInfo.remark_name" :disabled="!isEdit" />
      </el-form-item>

      <el-form-item label="炒群开启时间">
        <el-date-picker
          v-model="dataInfo.chao_run_start_time"
          type="datetime"
          placeholder="选择日期时间"
          format="yyyy-MM-dd HH:mm:ss"
          value-format="yyyy-MM-dd HH:mm:ss"
          :disabled="!isEdit"
          style="width: 260px"
        />
      </el-form-item>



      <el-form-item label="选择炒群脚本(如果炒群)">
          <el-select
            v-model="dataInfo.use_jiaoben_id"
            placeholder="请选择脚本"
            :disabled="!isEdit"
            style="width: 240px"
          >
            <el-option
              v-for="item in jiaoben"
              :key="item.id"
              :label="item.jiaoben_name"
              :value="item.id"
            />
          </el-select>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" style="width: 90px" @click="edit">
          {{ isEdit ? '保存' : '编辑' }}
        </el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { createChaoQunByGroupIdApi,getJiaoBenByGroupIdApi } from "@/api/ImChat";
import { getGroupId } from "@/utils/auth";

export default {
  data() {
    return {
      dataInfo: {
        use_jiaoben_id: null,
      },
      jiaoben:{},
      isEdit: true
    };
  },
  mounted() {
    // 页面加载时逻辑
    this.getJiaoBenByGroupId()
  },
  methods: {
    getJiaoBenByGroupId() {
      const data = { groupid: getGroupId() };
      getJiaoBenByGroupIdApi(data).then(res => {
        this.jiaoben = res.data.list;
      });
    },
    edit() {
      if (!this.isEdit) {
        this.isEdit = true;
        return;
      }

      const postData = {
        ...this.dataInfo,
        groupid: getGroupId(), // 确保携带 groupid
      };

      createChaoQunByGroupIdApi(postData).then(res => {
        if (res.code === 200) {
          this.$message.success('保存成功');
          this.isEdit = false;
        } else {
          this.$message.error(res.msg || '保存失败');
        }
      }).catch(() => {
        this.$message.error('请求失败，请检查网络或稍后再试');
      });
    }
  }
};
</script>

<style lang="scss" scoped>
.page-content {
  width: 100%;
  height: 100%;

  .header {
    padding-bottom: 15px;
    border-bottom: 1px solid #eee;

    h3 {
      font-size: 17px;
      font-weight: 500;
    }
  }

  /deep/ .el-form {
    .el-form-item {
      max-width: 700px;
    }
  }
}
</style>
